using System.Windows.Forms;
using Belmost4PDA.Constants;
using Belmost4PDA.Fasade;

namespace Belmost4PDA.Logic
{
    public class Control_ComboBox : Control_PanelControl
    {
        public class ComboBoxItem
        {
            public string ID;
            public string Text;
            public override string ToString()
            {
                return Text;
            }
        }
        private readonly HintCombo combobox = new HintCombo();
        private readonly QueryClsBuilder clsBuilder = null;
        public Control_ComboBox(PanelItem panelItem)
            : base(panelItem)
        {
           
                clsBuilder = new QueryClsBuilder(panelItem.ListSQL);
           
            Setup(panelItem);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="Control_ComboBox"/> class. Constructor fo testing only.
        /// </summary>
        /// <param name="panelItem">The item.</param>
        /// <param name="builder">The builder.</param>
        public Control_ComboBox(PanelItem panelItem, QueryClsBuilder builder)
            : base(panelItem)
        {
            clsBuilder = builder;
            Setup(panelItem);

        }

        private void Setup(PanelItem panelItem)
        {
            combobox.DropDownStyle = ComboBoxStyle.DropDownList;
            combobox.Tag = panelItem; 
            clsBuilder.Execute();
            while (!clsBuilder.Eof())
            {
                DataRows.DataRows dr = clsBuilder.Select();
                ComboBoxItem cbi=new ComboBoxItem();
                cbi.ID = dr.FieldByName("ID");
                cbi.Text = dr.FieldByName("TEXT");
                combobox.Items.Add(cbi);
            }
        }

        public override string GetControlType()
        {
            return ControlType.ComboBox;
        }
        public override AbstractMapper GetMapper()
        {
            return new Mapper_ComboBox(this);
        }

        public override Control GetFormControl()
        {
            return combobox;
        }

        public override string GetCaption()
        {
            return ((PanelItem)(combobox.Tag)).Caption;
        }
    }
}
